package com.zqxq.lottery.service.esports.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zqxq.lottery.entity.o2o.esports.ESportsO2O;
import com.zqxq.lottery.entity.po.esports.ESportsPO;
import com.zqxq.lottery.entity.po.sys.SysConfigPO;
import com.zqxq.lottery.entity.vo.esports.ESportsPlaywayVO;
import com.zqxq.lottery.entity.vo.esports.ESportsTitleVO;
import com.zqxq.lottery.entity.vo.esports.ESportsVO;
import com.zqxq.lottery.mapper.esports.ESportsMapper;
import com.zqxq.lottery.mapper.sys.SysConfigMapper;
import com.zqxq.lottery.service.esports.IESportsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class ESportsServiceImpl extends ServiceImpl<ESportsMapper, ESportsPO> implements IESportsService {
    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Override
    public ESportsVO getESportsById(String matchId) {
        ESportsPO eSportsPO = new ESportsPO();
        eSportsPO.setMatch_id(Long.valueOf(matchId));
        eSportsPO = this.baseMapper.selectOne(eSportsPO);
        ESportsVO vo =  ESportsO2O.toVO(eSportsPO);
        return vo;
    }

    @Override
    public List<ESportsTitleVO> getESportsTitleList(Integer type) {
        List<ESportsTitleVO> titleList = baseMapper.getESportsTitleList(type);
        return titleList;
    }

    @Override
    public List<ESportsVO> getESportsHomeList(Page<ESportsVO> page, Integer type) {
        List<Integer> typeList = new ArrayList();
        if(type!=null && (type==0 || type==99)){
            SysConfigPO sysConfigPO = sysConfigMapper.getByKey(String.valueOf(type));
            for(String typeStr : sysConfigPO.getCval().split(",")){
                typeList.add(Integer.valueOf(typeStr));
            }
        }else{
            typeList.add(type);
        }
        List<ESportsVO> homeList = baseMapper.getESportsHomeList(page, typeList);
        if (homeList != null){
            for (ESportsVO eSportsVO : homeList) {
                List<ESportsPlaywayVO> playVOList = getPlaywayListByMatchId(eSportsVO.getMatchId());
                eSportsVO.setPlayList(playVOList);
            }
        }
        return homeList;
    }

    public List<ESportsPlaywayVO> getPlaywayListByMatchId(String matchId) {
        List<ESportsPlaywayVO> playVOList = baseMapper.getPlaywayListByMatchId(matchId,null);
        for(ESportsPlaywayVO eSportsPlaywayVO : playVOList){
            Map<String,Object> playOddsMap = JSONObject.parseObject(eSportsPlaywayVO.getPlayOdds(),Map.class);
            eSportsPlaywayVO.setPlayOddsMap(playOddsMap);
            Integer mapSize = playOddsMap.size();
            if(mapSize>2){
                eSportsPlaywayVO.setShowOdds("0");
            }
        }
        return playVOList;
    }
    @Override
    public List<ESportsPlaywayVO> getPlaywayListByMatchId(String matchId,String round) {
        List<ESportsPlaywayVO> playVOList = baseMapper.getPlaywayListByMatchId2(matchId,round);
        for(ESportsPlaywayVO eSportsPlaywayVO : playVOList){
            Map<String,Object> playOddsMap = JSONObject.parseObject(eSportsPlaywayVO.getPlayOdds(),Map.class);
            eSportsPlaywayVO.setPlayOddsMap(playOddsMap);
            Integer mapSize = playOddsMap.size();
            if(mapSize>2){
                eSportsPlaywayVO.setShowOdds("0");
            }
        }
        return playVOList;
    }

}
